<template>
  <div>
      <van-row type="flex" justify="center">
        <van-col span="24">
          <van-search
            v-model="deviceId"
            show-action
            placeholder="请输入搜索设备号"
            @search="onSearch"
            @input="getInput"
          >
            <div slot="action" @click="onSearch">搜索</div>
          </van-search>
        </van-col>
      </van-row>
      <van-row type="flex" justify="center" v-for="(page, index) of pages" :key="index">
        <van-col span="20" v-for="item of page" :key="item.relay"  >
          <van-button @click="changingstate(index,item.relay)" :style="{'background':(item.relay==0?'#DC4E41':'#1AA15F')}" style="margin-bottom:20px" type="primary"  size="large">{{item.relay==0?"关":"开"}}</van-button>
        </van-col>
      </van-row>
      <van-row type="flex" justify="center"  >
        <van-col span="20" >
            <van-swipe class="my-swipe" :autoplay="3000" style="height: 150px;" vertical>
              <van-swipe-item>设备编号：{{deviceNumber}}</van-swipe-item>
              <van-swipe-item>设备状态：{{deviceState==0?"离线":"在线"}}</van-swipe-item>
              <van-swipe-item>{{allprojects}}</van-swipe-item>
              <van-swipe-item>{{wheelPlanting}}</van-swipe-item>
            </van-swipe>
        </van-col>
      </van-row>
  </div>
</template>
<script>
import { Search,Row,Col,Button,Toast,Swipe, SwipeItem } from 'vant'
export default {
  components: {
    [Search.name]: Search,
    [Row.name]: Row,
    [Col.name]: Col,
    [Button.name]: Button,
    [Toast.name]: Toast,
    [Swipe.name]: Swipe,
    [SwipeItem.name]: SwipeItem
  },
  name: 'HelloWorld',
  data () {
    return {
      value:'',
      deviceId:"1236853668896702466",
      allprojects:[
      ],
      timer: null,
      wheelPlanting:"",
      deviceState:"",
      deviceNumber:""
    }
  },
  computed: {
    pages () {
      const pages = []
      this.allprojects.forEach((item, index) => {
        const page = Math.floor(index / 1)//1代表1条为一行，随意更改
        if (!pages[page]) {
          pages[page] = []
        }
        pages[page].push(item)
      })
      console.log("pages"+JSON.stringify(pages) )
      return pages
    }
  },
  created(){
    this.onSearch()
  },
  activated(){
  },
  methods:{
    getInput(deviceId){
      //  console.log("value"+JSON.stringify(value) )
    },
    //获取设备信息
      onSearch() {
        let params={"deviceId":this.deviceId,"pushMessage":JSON.stringify({"type":"relay_read","Road":"1"})}
        this.$fetch('http://192.168.1.117:8080/iot-cloud/iot_device/msg/publishByDeviceId',params).then((response) => {
          this.zero()
        })
      },
      changingstate(index,state){
        var zhuangtai=""
        if(state==0){
           zhuangtai=1
        }else{
          zhuangtai=0
        }
        let params={"deviceId":this.deviceId,"pushMessage":JSON.stringify({"type":"relay_write","Road":index+1,"io":zhuangtai})}
        let _this=this
        this.$fetch('http://192.168.1.117:8080/iot-cloud/iot_device/msg/publishByDeviceId',params).then((response) => {
          if(response.success){
             var t;
              clearTimeout(t)
              t = setTimeout(function (){
                   _this.zero()
              }, 1800);
          }
        })
      },
      zero(){
        console.log("发送成功重新获取数据")
      let params={"deviceId":this.deviceId}
      this.$fetch('http://192.168.1.123:9001/iot-cloud/iot_device/getData',params)
            .then((response) => {
              if(response.result!=null){
                this.allprojects=[]
                for(var j=0;j<response.result.deviceInformation.length;j++){
                this.allprojects.push(response.result.deviceInformation[j])
                }
                this.wheelPlanting=response.result.wheelPlanting
                this.deviceState=response.result.deviceState
                this.deviceNumber=response.result.deviceNumber
              }else{
                  this.allprojects=[]
                  Toast.fail(response.message);
              }
            })
      }
  }
}
</script>
<style scoped>
  .my-swipe .van-swipe-item {
  color: #fff;
  font-size: 10px;
  line-height: 150px;
  text-align: center;
  background-color: #39a9ed;
}
</style>
